package com.ptswitch.material.pages.material;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.material.MaterialAttachment;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.MaterialAttachmentService;
import com.ptswitch.material.util.HlogConstants;

public class DeleteAttachment extends MaterialBasePage {
    @Inject
    private MaterialAttachmentService uploadSer;
    @InjectPage
    private ViewMaterial viewMaterial;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object delete(String id) {
        checkUserIsAdmins();
        MaterialAttachment upload = uploadSer.findById(id);
        getWebUtil().delete(upload);
        uploadSer.delete(upload);
        hlog(HlogConstants.deleteUpload, upload);
        return viewMaterial.initialize(upload.getOwnerId());
    }
}
